package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.PersonArticleActivity;
import com.tj.jinzao.adapter.MyFansListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.callback.IFallowView;
import com.tj.jinzao.model.FollowFan;
import com.tj.jinzao.presenter.FollowFanPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.List;

/**
 * 我的粉丝
 * Created by wjy on 2019/1/1.
 */
public class MyFansFragment extends BaseDataListFragment<List<FollowFan>, FollowFanPresenter, FollowFan> implements IFallowView<List<FollowFan>> {

    private MyFansListItemAdapter myFansListItemAdapter;

    public static MyFansFragment newInstance(int type) {
        MyFansFragment fragment = new MyFansFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(type));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.base_fragment_item_layout);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    protected FollowFanPresenter createPresenter() {
        return new FollowFanPresenter(this);
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }


    @Override
    protected void createView(LayoutInflater inflater, View view) {
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        myFansListItemAdapter=new MyFansListItemAdapter(mContext, mData,mTag);
        return myFansListItemAdapter;
    }

    @Override
    protected void requestData() {
        if (TextUtils.equals("1", mTag)) {
            mPresenter.getUserFans(mPageNo);
        } else {
            mPresenter.getUserFollow(mPageNo);
        }
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    protected boolean resolveData(List<FollowFan> users) {
        return false;
    }

    @Override
    public void onRequestSuccess(List<FollowFan> users) {
        super.onRequestSuccess(users);
        if (mPageNo <= 1) {
            mData.clear();
        }
        mData.addAll(users);
        showListView();
    }

    @Override
    protected void initData() {
        myFansListItemAdapter.setListener(new MyFansListItemAdapter.onClickLinstener() {
            @Override
            public void onClickAttent(long id) {
                mPresenter.followUser(id);
            }
        });
    }

    @Override
    protected void itemClicked(int position, FollowFan data) {
        if(data.getFollowUser()!=null){
            startActivity(new Intent(mContext, PersonArticleActivity.class)
                    .putExtra("user", data.getFollowUser()));

        }else {
            ToastUtils.showToast(getActivity(),"该用户不存在");
        }

    }

    @Override
    public void onRequestSuccessPage(int currentPage, int allPage, int allSize) {
    }

    @Override
    public void onReload(Object tag) {

    }

    @Override
    public void onAttentSuccess(List<FollowFan> followFans) {
        mPageNo=1;
        if (TextUtils.equals("1", mTag)) {
            mPresenter.getUserFans(mPageNo);
        } else {
            mPresenter.getUserFollow(mPageNo);
        }

    }

    @Override
    public void onAttentFailed(String msg) {
        if(msg==null){
            return;
        }
        ToastUtils.showToast(getActivity(),msg);

    }
}
